//
//  CLLiveHostView.m
//  chanlin
//
//  Created by Ryan on 15/6/13.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLLiveHostView.h"
#import "UIView+Utils.h"
#import "BlockUI.h"
#import "CLLiveViewController.h"
#import "ImageEdit.h"
#import "ChangyanSDK.h"
#import "CLComentDataModel.h"
#import "CLChatViewController.h"


@implementation CLLiveHostView

- (instancetype)init{
    self=[super init];
    if(self){
        [self setup];
    }
    return self;
}


- (void)setup{
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, GlobleWidth, 100);
    
    [self addSubview:button];
     __weak typeof(self) me=self;
    [button handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        if([self.titleField isFirstResponder]){
            [self.titleField resignFirstResponder];
        }
        
        if([self.textView isFirstResponder]){
            [self.textView resignFirstResponder];
        }
        [me removeFromSuperview];
    }];
    
    self.mareView=[UIView new];
    self.mareView.frame=CGRectMake(0, 100, GlobleWidth, GlobleHeight-100.f);
    self.mareView.backgroundColor=RGB(235.f, 235.f, 235.f);
    
    
    
    [self addSubview:self.mareView];
    
    UILabel *label=[[UILabel alloc] init];
    label.font=[UIFont systemFontOfSize:14.f];
    
    label.frame=CGRectMake(15.f, 16.f, 30.f, label.font.lineHeight);
    
    label.textColor=RGB(0x56, 0x56, 0x5e);
    
    label.text=@"标题";
    
    [self.mareView addSubview:label];
    
    self.titleField=[[UITextField alloc] init];
    self.titleField.font=[UIFont systemFontOfSize:12];
    self.titleField.textColor=RGB(0x4b, 0x4b, 0x4b);
    
    self.titleField.frame=CGRectMake(label.right+8.f, label.top-2, GlobleWidth-30.f-label.right, self.titleField.font.lineHeight+4);
    self.titleField.placeholder=@"请填写直播标题";
    [self.mareView addSubview:self.titleField];
    
    self.textView=[[UITextView alloc] init];
    self.textView.frame=CGRectMake(15.f, self.titleField.bottom+16.f, GlobleWidth-30.f, 80.f);
    [self.mareView addSubview:self.textView];
    self.textView.delegate=self;
    
    
    self.placeLabel=[UILabel new];
    self.placeLabel.font=[UIFont systemFontOfSize:12];
    self.placeLabel.textColor=RGB(0xc9, 0xc9, 0xc9);
    self.placeLabel.text=@"请您发表正知正见";
    self.placeLabel.frame=CGRectMake(4, 4, 100, self.placeLabel.font.lineHeight);
    [self.textView addSubview:self.placeLabel];
    
    [self.titleField becomeFirstResponder];
    
    
    
    label=[[UILabel alloc] init];
    label.font=[UIFont systemFontOfSize:14.f];
    
    label.frame=CGRectMake(15.f, self.textView.bottom+12.f, 30.f, label.font.lineHeight);
    
    label.textColor=RGB(0x56, 0x56, 0x5e);
    
    label.text=@"视频";
    
    [self.mareView addSubview:label];
    
    self.urlField=[[UITextField alloc] init];
    self.urlField.font=[UIFont systemFontOfSize:12];
    self.urlField.textColor=RGB(0x4b, 0x4b, 0x4b);
    
    self.urlField.frame=CGRectMake(label.right+8.f, label.top-2, GlobleWidth-30.f-label.right, self.urlField.font.lineHeight+4);
    self.urlField.placeholder=@"请填写视频地址";
    [self.mareView addSubview:self.urlField];

    
    self.sendButton=[UIButton buttonWithType:UIButtonTypeCustom];
    self.sendButton.layer.cornerRadius=0.5;
    [self.sendButton setBackgroundColor:RGB(251, 22, 44)];
    self.sendButton.frame=CGRectMake(234, self.urlField.bottom+12.f, 72.f, 28.f);
    [self.sendButton setTitle:@"发表" forState:UIControlStateNormal];
    [self.sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.mareView addSubview:self.sendButton];
    
   
    
    
    [self.sendButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        NSString *content=self.textView.text;
        NSString *title=self.titleField.text;
        NSString *video=self.urlField.text;
        if(_isStrNULL(content) && _isStrNULL(title)){
            [[CLGloble sharedInstance] showTieAlertMessage:@"评论数据不能为空" subMessage:@"请填写评论和标题"];
            return ;
        }
        
        if(_isStrNotNull(video) && !me.imageView.image){
            [[CLGloble sharedInstance] showTieAlertMessage:@"视频图片不能为空" subMessage:@"请填写视频图片"];
            return ;
            
        }
        
//        if([content length]>300){
//            [[CLGloble sharedInstance] showTieAlertMessage:@"评论不能超过300个字" subMessage:@"请修改"];
//            return;
//        }
        
        self.indicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [self.indicator startAnimating];
        self.indicator.center=CGPointMake(GlobleWidth/2.f, GlobleHeight/2.f-100);
      
        [self addSubview:self.indicator];
        if(me.imageView.image){
           UIImage *img= [ImageEdit scaleImageForSize:CGSizeMake(458*3.5, 344*3.5) Image:me.imageView.image];
            [ChangyanSDK uploadAttach:UIImageJPEGRepresentation(img, 0.8)
                        completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                            if(statusCode == CYSuccess){
                                
                                NSObject *obj=[[CLGloble sharedInstance] jsonFromString:responseStr];
                                NSString *aurl=[obj sgrGetStringForKey:@"url"];
                                NSMutableArray *array=@[aurl].mutableCopy;
                                [array sgrAddObject:video];
                                
                                
                                [ChangyanSDK submitComment:me.ctrl.chat1.topicId
                                                   content:[NSString stringWithFormat:@"%@#^%@",title,content]
                                                   replyID:nil
                                                     score:nil
                                                   appType:40
                                                   picUrls:@[aurl,video]
                                             completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                                                 
                                                 
                                                 sgrSafeMainThread(^{
                                                     [me.indicator stopAnimating];
                                                     [me.indicator removeFromSuperview];
                                                     if (statusCode == CYSuccess) {
                                                         [[CLGloble sharedInstance] showTieSuccessMessage:@"提示" subMessage:@"发送成功"];
                                                         [me removeFromSuperview];
                                                         
                                                     }else{
                                                         [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"发送失败"];
                                                     }

                                                 });
                                             }];
                                
                        
                               
                            }else{
                                [[CLGloble sharedInstance] showTieSuccessMessage:@"提示" subMessage:@"上传失败，请重试"];
                            }
                        }];
        }else{
        [[CLComentDataModel sharedInstance] submitComment:self.ctrl.chat1.topicId
                                                  content:[NSString stringWithFormat:@"%@#^%@",title,content]
                                                  replyID:nil
                                                    block:^(BOOL success, NSString *commentId) {
                                                        [me.indicator stopAnimating];
                                                        if (success) {
                                                            [[CLGloble sharedInstance] showTieSuccessMessage:@"提示" subMessage:@"发送成功"];
                                                            [me removeFromSuperview];
                                                           
                                                            
                                                        }else{
                                                            [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"发送失败"];
                                                        }
                                                      
                                                    }];
        }
        
        
        
    }];
    
    self.camareButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.camareButton setImage:[UIImage imageNamed:@"tb_cam.png"] forState:UIControlStateNormal];
    self.camareButton.contentEdgeInsets=UIEdgeInsetsMake(15, 15, 15, 15);
    self.camareButton.frame=CGRectMake(0, self.urlField.bottom, 46.f, 46.f);
   
    
    [self.camareButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
      
        [me.ctrl userSelectPicWithType2:UIImagePickerControllerSourceTypeCamera result:^(UIImage* image){
            
            if(image){
                me.imageView.image=image;
                [me.titleField becomeFirstResponder];
            }
        }];
    }];
    
    self.picButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.picButton setImage:[UIImage imageNamed:@"tb_pic.png"] forState:UIControlStateNormal];
    self.picButton.contentEdgeInsets=UIEdgeInsetsMake(15, 15, 15, 15);
    self.picButton.frame=CGRectMake(self.camareButton.right-15.f, self.urlField.bottom, 46.f, 46.f);
    [self.mareView addSubview:self.picButton];
     [self.mareView addSubview:self.camareButton];
    [self.picButton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [me.ctrl userSelectPicWithType2:UIImagePickerControllerSourceTypePhotoLibrary result:^(UIImage* image){
            if(image){
                me.imageView.image=image;
                [me.titleField becomeFirstResponder];
            }
            
        }];
    }];
    
    
    
    self.imageView=[UIImageView new];
    self.imageView .frame=CGRectMake(self.picButton.right, self.camareButton.top+15, 16.f, 16.f);
    [self.mareView addSubview:self.imageView];
    

    
}

- (void)textViewDidChange:(UITextView *)textView{
    if(textView.text==nil || [@"" isEqualToString:textView.text]){
        self.placeLabel.hidden=NO;
    }else{
        self.placeLabel.hidden=YES;
    }
}


@end
